package com.hibiscusking.system.service.impl;

import com.hibiscusking.system.domain.Menu;
import com.hibiscusking.system.mapper.MenuMapper;
import com.hibiscusking.system.service.IMenuService;
import com.hibiscusking.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hibiscusking
 * @since 2022-10-24
 */
@Service
public class MenuServiceImpl extends BaseServiceImpl<Menu> implements IMenuService {
    @Autowired
    private MenuMapper menuMapper;
    @Override
    public List<Menu> getMenuTree() {
        List<Menu> all = menuMapper.findAll();
        List<Menu> treeMenu = new ArrayList<>();
        for (Menu menu : all) {
            if (menu.getParentId() == null) {
                treeMenu.add(menu);
            } else {
                for (Menu menu1 : all) {
                    if (menu1.getId() == menu.getParentId()) {
                        List<Menu> children = menu1.getChildren();
                        children.add(menu);
                        break;
                    }
                }
            }
        }
        return treeMenu;
    }
}
